package com.cskaoyan.controller;

import com.cskaoyan.bean.AdminAdminListVo;
import com.cskaoyan.bean.MarketAdmin;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.AdminAdminService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 管理员管理的业务逻辑
 *
 * @author gatsby
 * @since 2022/10/11 19:39
 */
@RestController
@RequestMapping("admin/admin")
public class AdminAdminController {

    @Autowired
    AdminAdminService adminAdminService;

    /**
     * 显示全部管理员信息的业务逻辑
     *
     * @param basePageInfo 分页的基本数据
     * @param username     模糊查找时的用户名
     * @return com.cskaoyan.bean.common.BaseRespVo
     * @author gatsby
     * @since 2022/10/12 14:46
     */
    @RequestMapping("list")
    public BaseRespVo list(BasePageInfo basePageInfo, String username) {
        CommonData<AdminAdminListVo> commonData = adminAdminService.queryAllAdminWithPageLimit(basePageInfo, username);
        return BaseRespVo.ok(commonData, "成功");
    }

    /**
     * 添加管理员的业务逻辑
     *
     * @param marketAdmin 前台返来的管理员基本信息
     * @return com.cskaoyan.bean.common.BaseRespVo
     * @author gatsby
     * @since 2022/10/12 14:46
     */
    @PostMapping("create")
    public BaseRespVo createAdmin(@RequestBody MarketAdmin marketAdmin) {
        MarketAdmin admin = adminAdminService.createAdmin(marketAdmin);

        if (admin.getId() == -1) {
            return BaseRespVo.error("管理员已经存在");
        }
        if (admin.getId().equals(-2)) {
            return BaseRespVo.error("管理员密码不能小于6");
        }
        return BaseRespVo.ok(admin, "成功");
    }

    /**
     * 修改管理员信息的业务逻辑
     *
     * @param marketAdmin 前台返回的修改过的管理员基本信息
     * @return com.cskaoyan.bean.common.BaseRespVo
     * @author gatsby
     * @since 2022/10/12 14:49
     */
    @PostMapping("update")
    public BaseRespVo update(@RequestBody MarketAdmin marketAdmin) {
        MarketAdmin admin = adminAdminService.updateAdmin(marketAdmin);
        if (admin.getId() == -1) {
            return BaseRespVo.error("管理员已经存在");
        }
        if (admin.getId().equals(-2)) {
            return BaseRespVo.error("管理员密码不能小于6");
        }
        return BaseRespVo.ok(admin, "成功");
    }

    /**
     * 删除管理员的业务逻辑
     *
     * @param marketAdmin 前台返回的删除管理员的基本信息
     * @return com.cskaoyan.bean.common.BaseRespVo
     * @author gatsby
     * @since 2022/10/12 15:06
     */
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody MarketAdmin marketAdmin) {
            adminAdminService.deleteAdmin(marketAdmin);
            return BaseRespVo.ok("成功");
    }

}
